package com.xunk.system.controller;

import com.xunk.common.core.enums.DeviceEnum;
import com.xunk.common.core.utils.poi.ExcelUtil;
import com.xunk.common.core.web.controller.BaseController;
import com.xunk.common.core.web.domain.AjaxResult;
import com.xunk.common.core.web.page.TableDataInfo;
import com.xunk.common.log.annotation.Log;
import com.xunk.common.log.enums.BusinessType;
import com.xunk.system.domain.SysDeviceBaseInfo;
import com.xunk.system.service.ISysDeviceBaseInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 设备基础信息，按类别存储Controller
 *
 * @author xunk
 * @date 2021-04-25
 */
@RestController
@RequestMapping("/device/base")
public class SysDeviceBaseInfoController extends BaseController {
    @Autowired
    private ISysDeviceBaseInfoService sysDeviceBaseInfoService;

    /**
     * 查询设备基础信息，按类别存储列表
     */
    @PreAuthorize("@ss.hasPermi('buz:device:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDeviceBaseInfo sysDeviceBaseInfo) {
        startPage();
        List<SysDeviceBaseInfo> list = sysDeviceBaseInfoService.selectSysDeviceBaseInfoList(sysDeviceBaseInfo);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('buz:device:list')")
    @GetMapping("/type/list")
    public AjaxResult typeList() {
        List<Map<String, String>> collect = Arrays.stream(DeviceEnum.values()).map(v -> {
            Map<String, String> m = new HashMap<>();
            m.put("baseTypeCode", v.getDeviceType());
            m.put("baseType", v.getDeviceName());
            return m;
        }).collect(Collectors.toList());
        return AjaxResult.success(collect);
    }

    /**
     * 导出设备基础信息，按类别存储列表
     */
    @PreAuthorize("@ss.hasPermi('buz:device:export')")
    @Log(title = "设备基础信息，按类别存储", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDeviceBaseInfo sysDeviceBaseInfo) throws IOException {
        List<SysDeviceBaseInfo> list = sysDeviceBaseInfoService.selectSysDeviceBaseInfoList(sysDeviceBaseInfo);
        ExcelUtil<SysDeviceBaseInfo> util = new ExcelUtil<SysDeviceBaseInfo>(SysDeviceBaseInfo.class);
        util.exportExcel(response, list, "device");
    }

    /**
     * 获取设备基础信息，按类别存储详细信息
     */
    @PreAuthorize("@ss.hasPermi('buz:device:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(sysDeviceBaseInfoService.selectSysDeviceBaseInfoById(id));
    }

    /**
     * 新增设备基础信息，按类别存储
     */
    @PreAuthorize("@ss.hasPermi('buz:device:add')")
    @Log(title = "设备基础信息，按类别存储", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDeviceBaseInfo sysDeviceBaseInfo) {
        return toAjax(sysDeviceBaseInfoService.insertSysDeviceBaseInfo(sysDeviceBaseInfo));
    }

    /**
     * 修改设备基础信息，按类别存储
     */
    @PreAuthorize("@ss.hasPermi('buz:device:edit')")
    @Log(title = "设备基础信息，按类别存储", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDeviceBaseInfo sysDeviceBaseInfo) {
        return toAjax(sysDeviceBaseInfoService.updateSysDeviceBaseInfo(sysDeviceBaseInfo));
    }

    /**
     * 删除设备基础信息，按类别存储
     */
    @PreAuthorize("@ss.hasPermi('buz:device:remove')")
    @Log(title = "设备基础信息，按类别存储", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysDeviceBaseInfoService.deleteSysDeviceBaseInfoByIds(ids));
    }
}
